import request from '@/utils/request'
import { getStaticUserInfo } from '@/utils/user'
import { getDetail as getRole } from '@/api/role'
import { asyncRoutes } from '@/router'

export async function login(data) {
  /*const { type = '' } = data*/
  return request({
    url: `/adminlogin`,
    method: 'post',
    data,
  })
}

export async function socialLogin(data) {
  return request({
    url: '/socialLogin',
    method: 'post',
    data,
  })
}

export async function getUserRoles(data) {
  return new Promise((resolve) => {
    resolve([
      // 首页
      {
        key: 1,
        path: '/',
        name: 'Index',
        component: () => import('@/views/index'),
        meta: {
          title: '首页',
          affix: true,
          icon: 'home-2-line',
          roles: ['Index'],
        },
      },

      //项目管理
      {
        key: 2,
        path: '/manage',
        name: 'Manage',
        component: () => import('@/views/manage'),
        redirect: 'manageNotice',
        meta: {
          title: '项目管理',
          icon: 'calendar-line',
          roles: ['Manage'],
        },
        children: [
          // 公告管理
          {
            key: 21,
            path: '/manageNotice',
            name: 'ManageNotice',
            component: () => import('@/views/manage/notice'),
            meta: {
              title: '公告管理',
              roles: ['ManageNotice'],
            },
          },
          // 新闻管理
          {
            key: 22,
            path: '/manageNews',
            name: 'ManageNews',
            component: () => import('@/views/manage/news'),
            meta: {
              title: '新闻管理',
              roles: ['ManageNews'],
            },
          }, // 添加通知公告
          {
            key: 23,
            path: '/manageAddEdit',
            name: 'ManageAddEdit',
            component: () => import('@/views/manage/add_edit'),
            hidden: true,
            meta: {
              title: '新闻管理',
              roles: ['ManageAddEdit'],
            },
          },
        ],
      },

      //人员管理
      {
        key: 3,
        path: '/personnel',
        name: 'Personnel',
        component: () => import('@/views/personnel'),
        redirect: 'personnelCompany',
        meta: {
          title: '人员',
          icon: 'user-line',
          roles: ['Personnel'],
        },
        children: [
          // 公司成员
          {
            key: 31,
            path: '/personnelCompany',
            name: 'PersonnelCompany',
            component: () => import('@/views/personnel/company'),
            meta: {
              title: '公司成员',
              roles: ['PersonnelCompany'],
            },
          },
          // 公司成员
          {
            key: 32,
            path: '/personnelBusiness',
            name: 'PersonnelBusiness',
            component: () => import('@/views/personnel/business'),
            meta: {
              title: '供应商',
              roles: ['PersonnelBusiness'],
            },
          },
        ],
      },

      // 权限管理
      {
        key: 4,
        path: '/power',
        name: 'Power',
        component: () => import('@/views/power'),
        meta: {
          title: '权限管理',
          icon: 'bill-line',
          roles: ['Power'],
        },
      },
    ])
  })
  /*return request({
    url: '/getmenu',
    method: 'post',
    data,
  })*/
}

// TODO 换真实接口请求
export async function getUserInfo() {
  const staticUserInfo = getStaticUserInfo()
  const { type, role_id } = staticUserInfo
  //const ro = getUserRoles({ role_id: staticUserInfo.role_id })
  // roles 角色数组, 格式: ['父角色', '子角色'], 如: ['property'] 或 ['activity', 'xx']
  // 注: userInfo 接口需要直接返回该字段
  const arr = await getUserRoles({
    role_id: role_id,
  })
  let routes = JSON.parse(JSON.stringify(asyncRoutes))
  let permission = []
  for (let item of routes[0].children) {
    if (item.key && arr.role_id.includes(item.key)) {
      permission = permission.concat(item.meta.roles)
    }
    if (item.children) {
      for (let it of item.children) {
        if (it.key && arr.role_id.includes(it.key)) {
          permission = permission.concat(it.meta.roles)
        }
      }
    }
  }
  const roles = [type, ...permission]
  return {
    code: 1,
    data: { ...staticUserInfo, roles },
  }
}

export function logout() {
  const staticUserInfo = getStaticUserInfo()
  const { type } = staticUserInfo
  return request({
    url: `/loginOut`,
    method: 'post',
  })
}
